<?php
class resource_advertise_lang_concept extends system_concept_abstract {
	
	public function init() {
		$this->setModel('resource_advertise_lang_model', 'lang');
		$this->setModelCols('lang', array('item_id', 'lang', 'start_time', 'end_time'));
	}

	public function save($row) {
		$row->set('item_id', intval($row->get('item_id')));
		if(!$row->getItemId()) {
			throw new resource_exception("Item ID can not be empty.");
		}
		if(!$row->getStartTime() || !$row->getEndTime()) {
			throw new resource_exception("start time and end time can not be empty.");
		}
		if($row->getStartTime() > $row->getEndTime()) {
			throw new resource_exception("start time must be before end time.");
		}
		if(!system_setting_language::verify($row->getLang())) {
			throw new resource_exception("Language formation error.");
		}
		if(!$this->fetch(array('item_id = ?' => $row->getItemId(), 'lang = ?' => $row->getLang()))) {
			$this->getModel('lang')->insert($row->getModelData('lang'));
		} else {
			$row->update();
		}
		return $row;
	}
}